/**
 * @file app_main.c
 * @brief 
 * @author yejing (yejh@gongniu.cn)
 * @version 1.0
 * @date 2024-05-15
 * 
 * Copyright 2024-2025 GongNiu Inc. All Rights Reserved.
 * 
 */

#include <stdio.h>

#include "bull_hal_sys_thread.h"

#include "bull_hal_sys_memory.h"
#include "bull_hal_sys_mutex.h"
#include "bull_hal_sys_system.h"
#include "bull_hal_sys_queue.h"
#include "bull_hal_sys_thread.h"
#include "bull_hal_sys_semaphore.h"

#include "esp_log.h"

static const char* SYS_TAG = "sys_drv";

MUTEX_HANDLE tx_mutex = NULL ;
QUEUE_HANDLE tx_queue = NULL ;


/**
 * @brief 
 * 
 * @param param 
 */
static void app_init_thread(void* param)
{

    int num = 0;

    while ( 1 ){

        bull_hal_sys_queue_post( tx_queue, &num, 0 );

        bull_hal_sys_mutex_lock( tx_mutex );
        ESP_LOGI( SYS_TAG, "app_init_thread test_num:%d", num );
        bull_hal_sys_mutex_unlock( tx_mutex );

        bull_hal_sys_sleep( 500 );

        num++;

    }
}



/**
 * @brief 
 * 
 * @param param 
 */
static void app_init_thread2(void* param)
{
    uint32_t test_num = 0 ;

    while ( 1 ){

        if(bull_hal_sys_queue_fetch( tx_queue, &test_num, BULL_HAL_SYS_QUEUE_FOREVER ) == 0){

            ESP_LOGI( SYS_TAG, "app_init_thread2 test_num:%ld", test_num );
        }
    }
}

/**
 * @brief 
 * 
 */
static void app_init_entry(void)
{

    bull_hal_sys_mutex_create( &tx_mutex );
    bull_hal_sys_queue_create( &tx_queue, 20, 20  );

    bull_hal_sys_thread_create( app_init_thread,
    "app_init_thread",
    4*4096 ,
    NULL,
    10,
    NULL);

    bull_hal_sys_thread_create(app_init_thread2,
    "app_init_thread2",
    4*4096 ,
    NULL,
    9,
    NULL);
}

/**
 * @brief 
 * 
 */
void app_main(void)
{

    app_init_entry();
}
